package com.shop.goods.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.shop.common.annotation.Log;
import com.shop.common.core.controller.BaseController;
import com.shop.common.core.domain.AjaxResult;
import com.shop.common.enums.BusinessType;
import com.shop.goods.domain.ShopCart;
import com.shop.goods.service.IShopCartService;
import com.shop.common.utils.poi.ExcelUtil;
import com.shop.common.core.page.TableDataInfo;

/**
 * 购物车数据Controller
 *
 * @author wuhao
 * @date 2022-04-07
 */
@RestController
@RequestMapping("/goods/cart")
public class ShopCartController extends BaseController {
    @Autowired
    private IShopCartService shopCartService;

    /**
     * 查询购物车数据列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ShopCart shopCart) {
        startPage();
        List<ShopCart> list = shopCartService.selectShopCartList(shopCart);
        return getDataTable(list);
    }

    /**
     * 获取个人购物车数据
     *
     * @param shopCart
     * @return
     */
    @GetMapping("/mycart")
    public TableDataInfo list2(ShopCart shopCart) {
        startPage();
        List<ShopCart> list2 = shopCartService.selectMyCart(shopCart);
        return getDataTable(list2);
    }

    /**
     * 导出购物车数据列表
     */
    @Log(title = "购物车数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ShopCart shopCart) {
        List<ShopCart> list = shopCartService.selectShopCartList(shopCart);
        ExcelUtil<ShopCart> util = new ExcelUtil<ShopCart>(ShopCart.class);
        util.exportExcel(response, list, "购物车数据数据");
    }

    /**
     * 获取购物车数据详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(shopCartService.selectShopCartById(id));
    }

    /**
     * 新增购物车数据
     */
    @Log(title = "购物车数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ShopCart shopCart) {
        return toAjax(shopCartService.insertShopCart(shopCart));
    }

    /**
     * 客户端加入购物车
     */
    @PostMapping("/addCart")
    public AjaxResult addCart(@RequestBody ShopCart shopCart) {
        return toAjax(shopCartService.addCart(shopCart));
    }

    /**
     * 修改购物车数据
     */
    @Log(title = "购物车数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ShopCart shopCart) {
        return toAjax(shopCartService.updateShopCart(shopCart));
    }

    /**
     * 删除购物车数据
     */
    @Log(title = "购物车数据", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(shopCartService.deleteShopCartByIds(ids));
    }
}
